(function ($) {
	jQuery.extend( jQuery.easing, 
	{ 
		easeOutExpo: function (x, t, b, c, d) { 
			return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b; 
		}
	});
	$("#banner").append($("#banner").find('li:first').clone(true,true));
	setInterval(function () {
		
		$("#banner").animate({left: -1920},2000,"easeOutExpo",function () {
			$("#banner").find('li:first').remove();
			$("#banner").css('left', '0');
			$("#banner").append($("#banner").find('li:first').clone(true,true));
		});
	},5500);
	var OliWidth = 0;
	var currentLi = 0;
	var $OUL = $("div.tv_day_one_list ul");
	var OulLen = 0;
	var OliWidth1 = 0;
	var currentLi1 = 0;
	var $OUL1 = $("div.tv_live_list ul");
	var OulLen1 = 0;
	$(function () { 
		$.ajax({
			url: '../../server/src/sessionStatu.php', 
			dataType: 'json',
		}).then(function (res) {   
			if (res.status==1) { 
				var count = JSON.parse($.cookie('usercount'));
				insertCookie(count.uid);
				initList(count.uid);
				$("[name=offLine]").remove();
				$("#welcome").text('欢迎您，'+count.uname);
				$("#info_uname").text(count.uname);
				$("#info_avatar").prop({"src":"../../server/src/"+count.uimg})
				$("#left_user_info_online").show();
				$("#left_user_info").hide();
			}else{ 
				initTotal();
				$("[name=onLine]").remove();
			}
		})
		// console.log($("#exit"))
		$("#exit").on('click', function() {
			$.ajax({
				url: '../../server/src/exit.php',
				type: 'GET', 
                beforeSend: function(){
                    $(".loadingBox").show();
                }
			}).then(function (res) {
				var data = JSON.parse(res);
				if (data.status==1) {
					$(".loadingBox").hide(); 
					$.cookie("usercount","",{expires:-1});
					location.reload();
				}
			})
			return false;
		});

		$(".sysp").hover(function() {
			$(this).find('p').addClass('p_hover');
			$(this).next("dd").css('display', 'block');
		}, function() {
			$(this).find('p').removeClass('p_hover');
			$(this).next("dd").css('display', 'none');
			$(this).next("dd").hover(function() {
				$(".sysp").find('p').addClass('p_hover');
				$(this).css('display', 'block');
			}, function() {
				$(".sysp").find('p').removeClass('p_hover');
				$(this).css('display', 'none');
			});
		});

		$(".screen").hover(function() {
			$(".screen a").animate({opacity: '1'}, 500,'easeOutExpo');
		}, function() {
			$(".screen a").animate({opacity: '0'}, 500,'easeOutExpo');
		});

		$.ajax({
			url: '../../server/src/goodlist.php',
		}).then(function (res) { 
			$OUL.css('width', '0');
			$OUL1.css('width', '0');
			initDaily(res[5]);
			for (var i = 0; i < res.length; i++) {
				var item = res[i];
				var radomNum = parseInt(Math.random()*100);
				var radomNum1 = parseInt(Math.random()*100);
				var str = `<li>
		            <div class="three_box">
		              <a href="detail.html?g_id=${item.g_id}"  >
		                <div class="three_img_box">
		                  <img class="lazy-loading" src="${item.imglist[0]}" data-original="${item.imglist[0]}" style="display: block;">
		                </div>
		                <p class="three_box_tit c_clearfix">
		                  <em class="img_tit">${item.title}</em>
		                </p>
		                <p class="three_subtit">${item.description}</p>
		                <div class="three_yen">
		                  <span class="fl three_price"><i>¥</i>${item.newprice}</span>
		                  <span class="fl sale_price">
		                    <p class="i_pos_abs">
		                      <em class="i_tag i_zj">直降</em> 
		                    </p>
		                    <span class="fl five_star mt5"></span>
		                      <span class="or_p">平日价</span><em class="line_price"><i>¥</i>${item.price}</em>
		                    </span>
		                  <span class="fr purchased"><i>${radomNum}人</i>已购买</span>
		                </div>
		              </a>
		            </div>
		        </li>`;
		        $(".three_col ul").append($(str));
		        if (i%3==0) {
		        	var itemstr = `<li>`;
		        	itemstr += `<a href="detail.html?g_id=${item.g_id}">
                    <div class="tv_day_one_cc"><div class="tv_day_img_box">
                    <img src="${item.imglist[0]}"  data-original="${item.imglist[0]}">  </div>
                    <p class="tv_live_box_tit c_clearfix">
                    <em class="img_tit">${item.title}</em></p>
                    <div class="tv_live_box_yen"><span class="fl tv_live_box_price">
                    <i>¥</i>${item.newprice}</span> <em class="line_price"><i>¥</i>${item.price}</em>
                    </div><div class="tv_day_one_yen">
                    <span class="fl tv_live_box_surplus">还剩${radomNum1}件</span>
                    <p class=" fr close_tip cor_red_ing">马上抢</p></div></div></a>`;

		        	var itemstr1 = `<li>`;
		        	itemstr1 += `<div class="tv_live_cc" id="tv_live_1">
              		<div class="tv_live_img_box"> <i class="live_ing">TV直播中</i>
                	<img src="${item.icon}" class="tv_logo"> <a href="detail.html?g_id=${item.g_id}">
                    <img src="${item.imglist[0]}"  data-original="${item.imglist[0]}"> </a> 
              		</div> <a href="detail.html?g_id=${item.g_id}">  <p class="tv_live_box_tit c_clearfix">
                  	<em class="img_tit">${item.title}</em>  </p> <div class="tv_live_box_yen">
                  	<span class="fl tv_live_box_price"><i>¥</i>${item.newprice}</span>
                  	<span class="fl tv_live_box_sale_price"> <p class="i_pos_abs">
                  	 </p> </span>  </div> </a> </div>`;
		        }else if (i%3==2) { 
		        	itemstr += `<a href="detail.html?g_id=${item.g_id}">
                    <div class="tv_day_one_cc"><div class="tv_day_img_box">
                    <img src="${item.imglist[0]}"  data-original="${item.imglist[0]}">  </div>
                    <p class="tv_live_box_tit c_clearfix">
                    <em class="img_tit">${item.title}</em></p>
                    <div class="tv_live_box_yen"><span class="fl tv_live_box_price">
                    <i>¥</i>${item.newprice}</span> <em class="line_price"><i>¥</i>${item.price}</em>
                    </div><div class="tv_day_one_yen">
                    <span class="fl tv_live_box_surplus">还剩${radomNum1}件</span>
                    <p class=" fr close_tip cor_red_ing">马上抢</p></div></div></a></li>`;
                    var $Oli = $(itemstr);
                    $OUL.append($Oli);
                    OliWidth = parseInt($Oli.width()); 
                    $OUL.css({
                    	'width': parseInt($OUL.css('width'))+OliWidth,
                    	'left': '0px'
                    });
		        	itemstr1 += `<div class="tv_live_cc" id="tv_live_1">
              		<div class="tv_live_img_box"> <i class="live_ing">TV直播中</i>
                	<img src="${item.icon}" class="tv_logo"> <a href="detail.html?g_id=${item.g_id}">
                    <img src="${item.imglist[0]}"  data-original="${item.imglist[0]}"> </a> 
              		</div> <a href="detail.html?g_id=${item.g_id}">  <p class="tv_live_box_tit c_clearfix">
                  	<em class="img_tit">${item.title}</em>  </p> <div class="tv_live_box_yen">
                  	<span class="fl tv_live_box_price"><i>¥</i>${item.newprice}</span>
                  	<span class="fl tv_live_box_sale_price"> <p class="i_pos_abs">
                  	 </p> </span>  </div> </a> </div>`;
                  	 var $Oli1 = $(itemstr1);
                    $OUL1.append($Oli1);
                    OliWidth1 = parseInt($Oli1.width()); 
                    $OUL1.css({ 'width': parseInt($OUL1.css('width'))+OliWidth1, 'left': '0px'  });
		        }else{
		        	itemstr += `<a href="detail.html?g_id=${item.g_id}">
                    <div class="tv_day_one_cc"><div class="tv_day_img_box">
                    <img src="${item.imglist[0]}"  data-original="${item.imglist[0]}">  </div>
                    <p class="tv_live_box_tit c_clearfix">
                    <em class="img_tit">${item.title}</em></p>
                    <div class="tv_live_box_yen"><span class="fl tv_live_box_price">
                    <i>¥</i>${item.newprice}</span> <em class="line_price"><i>¥</i>${item.price}</em>
                    </div><div class="tv_day_one_yen">
                    <span class="fl tv_live_box_surplus">还剩${radomNum1}件</span>
                    <p class=" fr close_tip cor_red_ing">马上抢</p></div></div></a>`;

		        	itemstr1 += `<div class="tv_live_cc" id="tv_live_1">
              		<div class="tv_live_img_box"> <i class="live_ing">TV直播中</i>
                	<img src="${item.icon}" class="tv_logo"> <a href="detail.html?g_id=${item.g_id}">
                    <img src="${item.imglist[0]}"  data-original="${item.imglist[0]}"> </a> 
              		</div> <a href="detail.html?g_id=${item.g_id}">  <p class="tv_live_box_tit c_clearfix">
                  	<em class="img_tit">${item.title}</em>  </p> <div class="tv_live_box_yen">
                  	<span class="fl tv_live_box_price"><i>¥</i>${item.newprice}</span>
                  	<span class="fl tv_live_box_sale_price"> <p class="i_pos_abs">
                  	 </p> </span>  </div> </a> </div>`;
		        }
			}
		 	$("img.lazy-loading").lazyload({effect: "slideDown"}); 
		})
	})
	$("div#tv_day_one").on('click', '.d_prev', function(event) { 
		OulLen = $("div.tv_day_one_list ul li").length; 
		if (currentLi==0) {
			$OUL.animate({left: -OliWidth*(OulLen-1)}, 800);
			currentLi = OulLen-1;
		}else{
			$OUL.animate({left: -OliWidth*(currentLi-1)}, 800);
			currentLi = currentLi-1;
		}
	});
	$("div#tv_day_one").on('click', '.d_next', function(event) {
		OulLen = $("div.tv_day_one_list ul li").length; 
		if (currentLi==OulLen-1) {
			$OUL.animate({left: 0}, 800);
			currentLi = 0;
		}else{
			$OUL.animate({left: -OliWidth*(currentLi+1)}, 800);
			currentLi = currentLi+1;
		}
	});
	$("div#tv_live").on('click', '.t_prev', function(event) { 
		OulLen1 = $("div.tv_live_list ul li").length; 
		if (currentLi1==0) {
			$OUL1.animate({left: -OliWidth1*(OulLen1-1)}, 800);
			currentLi1 = OulLen1-1;
		}else{
			$OUL1.animate({left: -OliWidth1*(currentLi1-1)}, 800);
			currentLi1 = currentLi1-1;
		}
	});
	$("div#tv_live").on('click', '.t_next', function(event) {
		OulLen1 = $("div.tv_live_list ul li").length; 
		if (currentLi1==OulLen1-1) {
			$OUL1.animate({left: 0}, 800);
			currentLi1 = 0;
		}else{
			$OUL1.animate({left: -OliWidth1*(currentLi1+1)}, 800);
			currentLi1 = currentLi1+1;
		}
	});
	$("#__AD_head__shopcart").hover(function() {
		$("div.cart-bag").addClass('active');
		$(".cart-info").show();
	}, function() {
		$("div.cart-bag").removeClass('active');
		$(".cart-info").hide();
	});
	function initDaily (obj) {
		$("#daily-title").text(obj.title);
		$("#daily-description").text(obj.description);
		$("#daily-price").text(obj.price);
		$("#daily-newprice").text(obj.newprice);
		$("#daily-src").prop('src',obj.imglist[0]);
		$("#daily-href").prop('href','detail.html?g_id='+obj.g_id);
	}
})($) 
function insertCookie (uid) {  
	var shoppingcart = $.cookie('shoppingcart');
	if (shoppingcart) {
		shoppingcart = JSON.parse(shoppingcart); 
		var arr = [];
		for (let i = 0; i < shoppingcart.length; i++) {
			var item = shoppingcart[i];
			item.uid = uid; 
			var p = $.ajax({
				url: '../../server/src/addCart.php',
				type: 'POST',
				dataType: 'json',
				data: item,
			}).then(function (res) {
				initList(uid);
			})
			arr.push(p);
		}
		Promise.all(arr).then(function(res){ 
			$.cookie('shoppingcart',"",{expires:-1}); 
        })
	}
}
function resetShop (item) { 
	item.num = parseInt($("#goodsNumberInput").val());  
	var usercount = $.cookie('usercount'); 
	if(usercount){
		// 已登录 
		usercount = JSON.parse(usercount);
		item.uid = usercount.uid;
		 $.ajax({
			url: '../../server/src/addCart.php',
			type: 'POST',
			dataType: 'json',
			data: item,
		}).then(function (res) {
			initList(usercount.uid);
		})

	}else{
		// 未登录
		initCookieTotal(item);
	}
} 
function initList(id){ 
	$.ajax({
		url: '../../server/src/cartList.php',
		type: 'POST',
		dataType: 'json',
		data: {uid: id},
	}).then(function (res) { 
		var total = 0;
		res.forEach( function(item) {
			total += item.num;
		});
		$("#cart_num_1").text(total);
		$("#cart_num_2").text(total);
		setCart(res)
	})
	
}
function initTotal () {
	var total = 0; 
	var shoppingcart = $.cookie('shoppingcart') ; 
	if (shoppingcart) { 
		shoppingcart = JSON.parse(shoppingcart); 
		shoppingcart.forEach( function(cart) {
			total += cart.num; 
		});
		setCart(shoppingcart);
	} else{
		shoppingcart = JSON.parse("[]");
		setCart(shoppingcart);
		total = 0; 
	}
	$("#cart_num_1").text(total);
	$("#cart_num_2").text(total);
}
function initCookieTotal (item) {
	var total = 0; 
	var shoppingcart = $.cookie('shoppingcart');
	if (shoppingcart) {
		shoppingcart = JSON.parse(shoppingcart);
		var flag = false;
		for (var i = 0; i < shoppingcart.length; i++) {
			var ele = shoppingcart[i];
			if (ele.g_id==item.g_id) {
				ele.num = ele.num+item.num;
				break;
			}
		}
		if (i==shoppingcart.length) {
			shoppingcart.push(item);
		}
		console.log(shoppingcart);
		setCart(shoppingcart);
      	$.cookie('shoppingcart', JSON.stringify(shoppingcart), { expires: 7 });
		console.log(JSON.parse($.cookie("shoppingcart")));
	}else{
		shoppingcart=[item]; 
      	$.cookie('shoppingcart', JSON.stringify(shoppingcart), { expires: 7 });
		setCart(shoppingcart);
	} 
	shoppingcart.forEach( function(cart) {
		total += cart.num;
	});
	$("#cart_num_1").text(total);
	$("#cart_num_2").text(total);
}
function setCart (arr) { 
	$("#cartlist").html('');
	if (arr.length>0) {
		var num = 0;
		var total = 0;
		var str = `<div class="carlist"> <ul class="cartul">`;
		for (var i = 0; i < arr.length; i++) {
			var item = arr[i];
			str+=`<li> <a href="detail.html?g_id=${item.g_id}" target='_blank' class="clearfix"> <div class="cartimg l">
				<img alt="" src="${item.img}" width="50" height="50"> </div>
					<div class="cartlink">${item.title}</div>
					<div class="pricemx"> <span>¥${item.newprice}</span> x ${item.num} </div> </a></li>`;
			num+=item.num;
			total+=(item.num*item.newprice);
		}
		str+=` </ul> </div><div class="priczj"> <span class="canum">${num}</span>件商品 
		<span class="cartotal">¥<em>${total}</em></span> <a href="shoppingcart.html" class="gosum">去购物车结算</a></div>`;
		$("#cartlist").append($(str));
		
	}else{
		$("#cartlist").append($(`<p class="none_cart">购物车还没有商品，快去挑选吧~</p>`));
	}
}